//
//  ByViewDetailToolBar.m
//  oc_knowledge
//
//  Created by pro on 17/1/20.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewProductDetailToolBar.h"

@implementation ByViewProductDetailToolBar
{
    NSArray *_mainTitles;
    NSArray *_mainFgColors;
    NSArray *_mainBgColors;
    NSArray *_subTitles;
    NSArray *_subImages;
    CGFloat _mainRatio;
}
- (instancetype)initWithFrame:(CGRect)frame
                   mainTitles:(NSArray *)mainTitles
                 mainFgColors:(NSArray *)mainFgColors
                 mainBgColors:(NSArray *)mainBgColors
                    mainRatio:(CGFloat)mainRatio
                    subTitles:(NSArray *)subTitles
                    subImages:(NSArray *)subImages {
    
    if (self = [super initWithFrame:frame]) {
        
        _mainTitles     = mainTitles;
        _mainFgColors   = mainFgColors;
        _mainBgColors   = mainBgColors;
        _mainRatio      = mainRatio;
        _subTitles      = subTitles;
        _subImages      = subImages;
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self setupUI];
        });
    }
    return self;
}

- (void)setupUI {
    
    // 创建子功能视图块
    NSAssert((_subTitles || _subImages), @"sub_titles or sub_images need one at least");
    // 子功能块的个数
    NSInteger subCount = _subTitles ? _subTitles.count : _subImages.count;
    // 子功能块所能占有的宽度
    CGFloat subWidth = self.width * (1 - _mainRatio);
    CGFloat x, y, width, height;
    for (int i = 0; i < subCount; i ++) {
//        // 线条
//        if (i != 0) {
//            x = (subWidth / subCount) * i;
//            y = 0;
//            width = 0.5;
//            height = self.height;
//            UIView *line = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
//            line.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
//            [self addSubview:line];
//        }
        
        // 父视图
        width = subWidth / subCount;
        height = self.height;
        x = width * i;
        y = 0;
        UIControl *basalView = [[UIControl alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [basalView addTarget:self action:@selector(functionClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:basalView];
        basalView.tag = 10 + i;
        // 图片
        x = 0;
        y = 0;
        width = MIN(basalView.width, basalView.height) * 0.4;
        height = width;
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [basalView addSubview:imgView];
        imgView.tag = 100;
        // 文字
        x = 0;
        y = 0;
        width = basalView.width;
        height = 12;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        label.adjustsFontSizeToFitWidth = YES;
        label.font = FONT(10);
        label.textColor = [UIColor grayColor];
        label.textAlignment = NSTextAlignmentCenter;
        [basalView addSubview:label];
        label.tag = 200;
        
        // 根据文字数组和图片数组的有无，来设置位置和内容
        // 都存在
        if (_subTitles && _subImages) {
            // 设置内容
            imgView.image = [UIImage imageNamed:_subImages[i]];
            label.text = _subTitles[i];
            // 设置位置
            CGFloat margin = 3;
            CGFloat remain = (basalView.height - imgView.height - label.height - margin) / 2;
            imgView.top = remain;
            imgView.x = basalView.width / 2;
            label.top = imgView.bottom + margin;
        }
        // 仅存在一个，都不存在的情况，已经被视为异常抛出了（故这里可以不用考虑）
        else {
            if (_subTitles) {
                label.text = _subTitles[i];
                label.font = FONT(14);
                label.width = basalView.width;
                label.height = basalView.height;
                
                imgView.hidden = YES;
            } else {
                imgView.image = [UIImage imageNamed:_subImages[i]];
                imgView.width = MIN(basalView.width, basalView.height) * 0.5;
                imgView.height = imgView.width;
                imgView.x = basalView.width / 2;
                imgView.y = basalView.height / 2;
                
                label.hidden = YES;
            }
        }
    }
    
    
    CGFloat mainWidth = self.width * _mainRatio;
    for (int i = 0; i < _mainTitles.count; i ++) {
        width = mainWidth / _mainTitles.count;
        height = self.height;
        x = subWidth + width * i;
        y = 0;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(x, y, width, height);
        button.backgroundColor = _mainBgColors[i];
        button.titleLabel.font = FONT(14);
        [button setTitle:_mainTitles[i] forState:UIControlStateNormal];
        [button setTitleColor:_mainFgColors[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(functionClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        button.tag = 10 + subCount + i;
    }
}


- (void)functionClick:(UIControl *)sender {
    
    sender.selected = !sender.selected;
    if (self.functionClickBlock) {
        self.functionClickBlock(sender.tag - 10, sender.selected);
    }
}

// 对于子功能块的状态改变（比如收藏）
- (void)subFunctionStatusModifyWithIndex:(NSInteger)index
                           currentStatus:(BOOL)status
                            currentTitle:(NSString *)title
                           currtentImage:(NSString *)imageName {
    
    NSInteger tag = index + 10;
    
    UIControl *basalView = [self viewWithTag:tag];
    basalView.selected = status;
    
    UIImageView *imgView = [basalView viewWithTag:100];
    UILabel *label = [basalView viewWithTag:200];
    imgView.image = [UIImage imageNamed:imageName];
    label.text = title;

}

@end
